package com.wontube.core.remote.session;

import java.util.List;

import com.wontube.client.extension.remote.session.ISessionHandler;
import com.wontube.client.modle.vo.ClientSession;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.SessionManager;
import com.wontube.core.pub.servlet.handlers.BasicHandler;

@NoLoginSupport
public class SessionHandler extends BasicHandler implements ISessionHandler {
	@Override
	public String getSessionId() {
		String sessionId = super.getContext().getSession().getSessionId();
		return sessionId;
	}
	
	/* 
	 * @see com.wontube.smr.web.client.ISessionHandler#getAllSession()
	 */
	@Override
	public List<ClientSession> listSessions() {
		List<ClientSession> list = SessionManager.manager.listSessions();
		return list;
	}
	
	/*
	 * @see com.wontube.client.extension.remote.session.ISessionHandler#getSessionCount()
	 */
	@Override
	public int getSessionCount() {
		return SessionManager.manager.getSessionCount();
	}
	
	@Override
	public List<ClientSession> listSessions(String agent) {
		return SessionManager.manager.listSessionsByAgent(agent);
	}

	/* 
	 * @see com.wontube.smr.web.client.ISessionHandler#getProperty(java.lang.String)
	 */
	@Override
	public <T> T getProperty(String key) {
		String sessionId = super.getContext().getSession().getSessionId();
		ClientSession session = SessionManager.manager.getSession(sessionId);

		return session.<T>getProperty(key);
	}

	/* 
	 * @see com.wontube.smr.web.client.ISessionHandler#putProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public <T> boolean putProperty(String key, T value) {
		String sessionId = super.getContext().getSession().getSessionId();
		ClientSession session = SessionManager.manager.getSession(sessionId);
		session.putProperty(key, value);
		SessionManager.manager.putSession(session.getSessionId(), session);
		
		return true;
	}
}
